import 'package:flutter/material.dart';
import 'package:oster_cigarette/common/const/const.dart';
import 'package:oster_cigarette/router/base_page.dart';

class HomePage extends RoutePage {
  const HomePage({Key? key}) : super(key: key);
  @override
  _HomePageState createState() => _HomePageState();
}

class _HomePageState extends PageState<HomePage> {
  int currentIndex = 0;
  List<Widget> pages = <Widget>[Container(), Container(), Container(), Container()];
  List<IconData> iconData = <IconData>[Icons.home, Icons.camera, Icons.shopping_cart_rounded, Icons.person_outline];
  static const List<String> tabTitles = ["首页", "发现", "购物车", '我的'];
  final PageController _pageController = PageController();
  List<BottomNavigationBarItem> _createTabarList() {
    return tabTitles
        .map(
          (e) => BottomNavigationBarItem(
            icon: Icon(
              iconData[tabTitles.indexOf(e)],
              color: Colors.grey,
            ),
            activeIcon: Icon(iconData[tabTitles.indexOf(e)], color: Colours.mainColor),
            label: e,
          ),
        )
        .toList();
  }

  void _tabIndexChanged(int index) {
    _pageController.jumpToPage(index);
    setState(() {
      currentIndex = index;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: PageView(
        physics: const NeverScrollableScrollPhysics(),
        controller: _pageController,
        children: pages,
      ),
      resizeToAvoidBottomInset: false,
      bottomNavigationBar: BottomNavigationBar(
        type: BottomNavigationBarType.fixed,
        currentIndex: currentIndex,
        selectedItemColor: Colours.mainColor,
        selectedFontSize: 14,
        unselectedFontSize: 14,
        showUnselectedLabels: true,
        items: _createTabarList(),
        onTap: _tabIndexChanged,
      ),
    );
  }
}
